package com.sgb.contract.view.ui.contract;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.BindingAdapter;

import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.model.ContractListSearchBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.xrecyclerview.XRecyclerView;

/**
 * 描述：合同-列表适配器
 *
 * @author qiangHeNoo
 * <p>
 * 时间： 2021.11.05
 */
public class ContractItemAdapter extends BaseRecycleAdapter<ContractListSearchBean.ContractListInfo> {

    @SuppressLint("StaticFieldLeak")
    public static XRecyclerView recyclerView;

    private OnClickListener listener;

    public ContractItemAdapter(Context context) {
        super(context, R.layout.contract_adapter_item_contract, null);
    }

    @BindingAdapter("bindAdapter1")
    public static void recycleViewBindAdapter(XRecyclerView xRecyclerView, XRecyclerView.Adapter<ItemViewHolder> adapter) {
        xRecyclerView.setAdapter(adapter);
        recyclerView = xRecyclerView;
    }

    @Override
    public void convert(ItemViewHolder holder, ContractListSearchBean.ContractListInfo data) {
        holder.setBinding(BR.data, data);
        holder.setBinding(BR.adapter, this);
        holder.itemView.setOnClickListener(v -> {
            if (null != this.listener) {
                this.listener.onClick(data);
            }
        });
    }

    /**
     * status 合同状态 1:拟定中，2：签署中，3：已完成，4：已过期，5，已作废
     *
     * @param bean ContractListSearchBean.ContractListInfo
     * @return Drawable
     */
    public Drawable setContractStateImg(ContractListSearchBean.ContractListInfo bean) {
        if (1 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state1, null);
        } else if (2 == bean.status && 6 == bean.appSignTag) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state2, null);
        } else if (2 == bean.status && 7 == bean.appSignTag) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state3, null);
        } else if (3 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state4, null);
        } else if (4 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state5, null);
        } else {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state6, null);
        }
    }

    public int setContractApprovalStatusColor(ContractListSearchBean.ContractListInfo bean) {
        if (bean.appAuditStatus == 0) {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_ff333333, null);
        } else if (1 == bean.appAuditStatus) {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_5792FD, null);
        } else if (2 == bean.appAuditStatus) {
            return ResourcesCompat.getColor(context.getResources(), R.color.contract_color_06C764, null);
        } else if (3 == bean.appAuditStatus) {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_EF4033, null);
        } else {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_ff333333, null);
        }
    }

    public String setContractApprovalStatusValue(ContractListSearchBean.ContractListInfo bean) {
        if (bean.appAuditStatus == 0) {
            return "/";
        } else if (1 == bean.appAuditStatus) {
            return "审批中";
        } else if (2 == bean.appAuditStatus) {
            return "已通过";
        } else if (3 == bean.appAuditStatus) {
            return "已拒绝";
        } else {
            return "/";
        }
    }

    public void setOnclickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void refreshComplete() {
        recyclerView.refreshComplete();
    }

    public void loadMoreComplete() {
        recyclerView.loadMoreComplete();
    }

    public void setNoMore(boolean isNoMore) {
        recyclerView.setNoMore(isNoMore);
    }

    public interface OnClickListener {
        void onClick(ContractListSearchBean.ContractListInfo bean);
    }
}
